package club.emergency.wechat.resource_study.operate.consumer.controller;

import club.emergency.wechat.common.response.ResponseEntity;
import club.emergency.wechat.common.response.StatusCode;
import club.emergency.wechat.common.utils.JsonConvert;
import club.emergency.wechat.model.resource_study.ResourceStudy;
import club.emergency.wechat.resource_study.operate.consumer.service.ResourceStudyOperateConsumer;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Objects;

/**
 * Create with IntelliJ IDEA
 * Project Name: business
 * Package Name: club.emergency.safetytraining.resource_study.operate.consumer.controller
 * Date: 2018/12/21
 *
 * @author: 快乐的小菌菌
 * Description:
 */
@Api("资料学习操作模块")
@RestController
@RequestMapping("/resource-study-operate")
public class ResourceStudyOperateController {

    private ResourceStudyOperateConsumer resourceStudyOperateConsumer;

    public ResourceStudyOperateController(ResourceStudyOperateConsumer resourceStudyOperateConsumer) {
        this.resourceStudyOperateConsumer = resourceStudyOperateConsumer;
    }

    @ApiOperation("上传文件接口")
    @PostMapping("/uploadFile")
    public ResponseEntity uploadFile(MultipartFile file) {
        if (Objects.isNull(file)) {
            return new ResponseEntity(StatusCode.UPLOAD_FILE_ERROR, "传入文件为空，请检查！");
        }
        String fileName = file.getOriginalFilename();
        int i = Objects.requireNonNull(fileName).lastIndexOf(".");
        String extName = fileName.substring(i + 1);
        String upload = null;
        try {
            byte[] bytes = file.getBytes();
            if (bytes.length == 0) {
                return new ResponseEntity(StatusCode.UPLOAD_FILE_ERROR, "传入文件长度为0，请检查！");
            }
            upload = resourceStudyOperateConsumer.uploadFile(bytes, extName, fileName);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return new JsonConvert<ResponseEntity>().jsonDecode(upload, ResponseEntity.class);
    }

    @ApiOperation("删除文件接口")
    @ApiImplicitParam(name = "id", value = "文件id", required = true, dataType = "string", paramType = "query")
    @PostMapping("/deleteFile")
    public ResponseEntity deleteFile(@RequestParam("id") Long id) {
        return new JsonConvert<ResponseEntity>().jsonDecode(resourceStudyOperateConsumer.deleteFile(id), ResponseEntity.class);
    }

    @ApiOperation("新增资料学习数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "resourceName", value = "资料名称", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "resourceType", value = "资料类型（0 pdf,1 视频）", required = true, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "resourceFileId", value = "资料文件id", required = true, dataType = "long", paramType = "query"),
            @ApiImplicitParam(name = "resourceFilePath", value = "资料文件路径", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "size", value = "文件大小", required = true, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "resourceCategory", value = "资料种类", required = true, dataType = "string", paramType = "query")
    })
    @PostMapping("/save")
    public ResponseEntity save(
            @RequestParam("resourceName") String resourceName,
            @RequestParam("resourceType") Integer resourceType,
            @RequestParam("resourceFileId") Long resourceFileId,
            @RequestParam("resourceFilePath") String resourceFilePath,
            @RequestParam("size") Integer size,
            @RequestParam("resourceCategory") Long resourceCategory
    ) {
        ResourceStudy resourceStudy = new ResourceStudy();
        resourceStudy.setResourceName(resourceName);
        resourceStudy.setResourceType(resourceType);
        resourceStudy.setResourceFileId(resourceFileId);
        resourceStudy.setResourceFilePath(resourceFilePath);
        resourceStudy.setSize(size);
        resourceStudy.setResourceCategory(resourceCategory);
        return new JsonConvert<ResponseEntity>().jsonDecode(resourceStudyOperateConsumer.save(resourceStudy), ResponseEntity.class);
    }

    @ApiOperation("修改资料学习数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "long", paramType = "query"),
            @ApiImplicitParam(name = "resourceName", value = "资料名称", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "resourceType", value = "资料类型（0 pdf,1 视频）", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "resourceFileId", value = "资料文件id", dataType = "long", paramType = "query"),
            @ApiImplicitParam(name = "resourceFilePath", value = "资料文件路径", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "size", value = "文件大小", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "resourceCategory", value = "资料种类", dataType = "string", paramType = "query")
    })
    @PostMapping("/modify")
    public ResponseEntity modify(
            @RequestParam("id") Long id,
            @RequestParam(value = "resourceName", required = false) String resourceName,
            @RequestParam(value = "resourceType", required = false) Integer resourceType,
            @RequestParam(value = "resourceFileId", required = false) Long resourceFileId,
            @RequestParam(value = "resourceFilePath", required = false) String resourceFilePath,
            @RequestParam(value = "size", required = false) Integer size,
            @RequestParam(value = "resourceCategory", required = false) Long resourceCategory
    ) {
        ResourceStudy resourceStudy = new ResourceStudy();
        resourceStudy.setId(id);
        resourceStudy.setResourceName(resourceName);
        resourceStudy.setResourceType(resourceType);
        resourceStudy.setResourceFileId(resourceFileId);
        resourceStudy.setResourceFilePath(resourceFilePath);
        resourceStudy.setSize(size);
        resourceStudy.setResourceCategory(resourceCategory);
        return new JsonConvert<ResponseEntity>().jsonDecode(resourceStudyOperateConsumer.modify(resourceStudy), ResponseEntity.class);
    }

    @ApiOperation("删除资料学习数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "long", paramType = "query"),
            @ApiImplicitParam(name = "resourceFileId", value = "资料文件id", dataType = "long", paramType = "query")
    })
    @PostMapping("/remove")
    public ResponseEntity remove(
            @RequestParam("id") Long id,
            @RequestParam(value = "resourceFileId", required = false) Long resourceFileId
    ) {
        ResourceStudy resourceStudy = new ResourceStudy();
        resourceStudy.setId(id);
        resourceStudy.setResourceFileId(resourceFileId);
        return new JsonConvert<ResponseEntity>().jsonDecode(resourceStudyOperateConsumer.remove(resourceStudy), ResponseEntity.class);
    }
}
